package cn.ultrarisk.scadmin.manager.biz.monitor.dao;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.entity.SolrReplicaMonitor;
import cn.ultrarisk.scadmin.manager.biz.solr.context.SolrState;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * solr集合节点监控数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class SolrReplicaMonitorDAO extends BaseDAO<SolrReplicaMonitor, Long> {
    /**
     * 构造器
     */
    public SolrReplicaMonitorDAO() {
        super(SolrReplicaMonitor.class, SolrReplicaMonitor.COLUMN_PROPERTY_MAP);
    }

    public Map<Integer, SolrReplicaMonitor> groupByReplicaId(long startTime, long endTime) {
        Query query = Query.gteQuery(SolrReplicaMonitor.CREATE_TIME, startTime).lt(SolrReplicaMonitor.CREATE_TIME, endTime);
        query.addGroup(SolrReplicaMonitor.REPLICA_ID);
        return findMap(query, SolrReplicaMonitor.REPLICA_ID, SolrReplicaMonitor.NODE_ID, SolrReplicaMonitor.COLLECTION_ID, SolrReplicaMonitor.SHARD_ID, SolrReplicaMonitor.REPLICA_ID);
    }

    public List<SolrReplicaMonitor> getGrowStat(long startTime, long endTime) {
        Query query = Query.gteQuery(SolrReplicaMonitor.CREATE_TIME, startTime).lt(SolrReplicaMonitor.CREATE_TIME, endTime);
        return sqlSessionTemplate.selectList(entityName + ".growStat", query.toMap(tableName));
    }

    public Map<SolrReplicaMonitor, Map<String, Integer>> getStatusStat(long startTime, long endTime) {
        Map<Integer, SolrReplicaMonitor> monitorMap = groupByReplicaId(startTime, endTime);
        if (monitorMap.size() > 0) {
            Query query = Query.gteQuery(SolrReplicaMonitor.CREATE_TIME, startTime).lt(SolrReplicaMonitor.CREATE_TIME, endTime);
            query.addAttach("replicaIds", monitorMap.keySet());
            query.addAttach("states", SolrState.values());
            Map<String, Object> statusStat = sqlSessionTemplate.selectOne(entityName + ".statusStat", query.toMap(tableName));

            Map<SolrReplicaMonitor, Map<String, Integer>> stat = new HashMap<SolrReplicaMonitor, Map<String, Integer>>();
            for (String key : statusStat.keySet()) {
                String[] arr = key.split("_");
                Integer replicaId = Integer.parseInt(arr[arr.length - 1]);
                SolrReplicaMonitor replica = monitorMap.get(replicaId);
                Map<String, Integer> replicaStatusMap = stat.get(replica);
                if (replicaStatusMap == null) {
                    replicaStatusMap = new HashMap<String, Integer>();
                }
                replicaStatusMap.put(key.replace("_" + replicaId, ""), Integer.parseInt(statusStat.get(key).toString()));
                stat.put(replica, replicaStatusMap);
            }
            return stat;
        }
        return null;
    }

    public Map<Integer, Long> getNewestReplicaDocs(List<Integer> replicaIds) {
        Query query = Query.inQuery(SolrReplicaMonitor.REPLICA_ID, replicaIds);
        List<Map<String, Object>> list = sqlSessionTemplate.selectList(entityName + ".getNewestReplicaDocs", query.toMap(tableName));
        Map<Integer, Long> docsMap = new HashMap<Integer, Long>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Map<String, Object> map : list) {
                Integer collectionId = Integer.parseInt(map.get(SolrReplicaMonitor.REPLICA_ID).toString());
                Long docs = Long.parseLong(map.get(SolrReplicaMonitor.NUM_DOCS).toString());
                docsMap.put(collectionId, docs);
            }
        }
        return docsMap;
    }

    public Map<Integer, Long> getNewestCollectionDocs(List<Integer> collectionIds) {
        Query query = Query.inQuery(SolrReplicaMonitor.COLLECTION_ID, collectionIds);
        List<Map<String, Object>> list = sqlSessionTemplate.selectList(entityName + ".getNewestCollectionDocs", query.toMap(tableName));
        Map<Integer, Long> docsMap = new HashMap<Integer, Long>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Map<String, Object> map : list) {
                Integer collectionId = Integer.parseInt(map.get(SolrReplicaMonitor.COLLECTION_ID).toString());
                Long docs = Long.parseLong(map.get(SolrReplicaMonitor.NUM_DOCS).toString());
                docsMap.put(collectionId, docs);
            }
        }
        return docsMap;
    }
}
